//
//  FPMovieDetailViewController.m
//  FilmParadise
//
//  Created by lanou3g on 15/7/17.
//  Copyright (c) 2015年 DLYgroup. All rights reserved.
//
#define kDiscussUrl @"http://api.m.mtime.cn/Movie/HotLongComments.api?movieId=%@&pageIndex=1"

#define kdirctotUrl @"http://api.m.mtime.cn/Movie/MovieCreditsWithTypes.api?movieId=%@"
#import "FPMovieDetailViewController.h"
#import "FPDetailMovie.h"
#import "UMSocial.h"
#import "MBProgressHUD.h"
#import "FPNetWork.h"
#import "LORequestManger.h"
#import "FPDisscussModel.h"
#import "FPdirectorModel.h"
#import "FPdirectorTableViewCell.h"
#import "FPDiscussTableViewCell.h"
@interface FPMovieDetailViewController ()<UMSocialUIDelegate, UITableViewDelegate, UITableViewDataSource>
@property (nonatomic , strong)FPDetailMovie *detailV;
@property (nonatomic , strong)UIView *statusBarView;
@property (nonatomic , strong)MBProgressHUD *HUD;
@property (nonatomic , strong)NSMutableArray *discussArr;
@property (nonatomic , strong)FPdirectorModel *direModel;
@end

@implementation FPMovieDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setview];
    [self addTarget];
    [self setUrl];
    
}
- (void)setUrl
{
    self.HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [self.view bringSubviewToFront:_HUD];
    
    if ([[FPNetWork shareInstance] rechability])
    {
        self.HUD.labelText = @"正在加载";
        [self setAllUrlWith:[NSString stringWithFormat:kDiscussUrl, _viewModel.movieID]];
        [self setAllUrlWith:[NSString stringWithFormat:kdirctotUrl,_viewModel.movieID]];
    }
    
    
    
}
- (void)setAllUrlWith:(NSString *)str
{
    [LORequestManger GET:str success:^(id response) {
        if ([str isEqualToString:[NSString stringWithFormat:kDiscussUrl, _viewModel.movieID]]) {
           // NSLog(@"%@",[NSString stringWithFormat:kDiscussUrl, _viewModel.movieID]);
            NSArray *array = response[@"comments"];
            self.discussArr = [NSMutableArray array];
            for (NSDictionary *smDic in array) {
                FPDisscussModel *disMod = [FPDisscussModel discussModleWithDic:smDic];
                
                [_discussArr addObject:disMod];
            }
        }
        
        if ([str isEqualToString:[NSString stringWithFormat:kdirctotUrl,_viewModel.movieID]]) {
            self.direModel = [FPdirectorModel directorModelWithDic:response];
            
            
        }
        [self.detailV.tableView reloadData];
        
        
        [self.HUD hide:YES afterDelay:1];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"ddddd");
    }];
    
    
    
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0)
    {
        return 1;
    }
    return self.discussArr.count;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return 360;
    }
    return 130;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *ID  = @"cell";
    static NSString *ID2 = @"cell1";
    if (indexPath.section == 0) {
        FPdirectorTableViewCell *directorcell = [tableView dequeueReusableCellWithIdentifier:ID forIndexPath:indexPath];
        [self setCellValueWithCell:directorcell];
        directorcell.selectionStyle = UITableViewCellSelectionStyleNone;
        return directorcell;
    }
    else
    {
        FPDiscussTableViewCell *discell = [tableView dequeueReusableCellWithIdentifier:ID2 forIndexPath:indexPath];
        [self setDisValueWithCell:discell indexpath:indexPath];
        discell.selectionStyle = UITableViewCellSelectionStyleNone;

        return discell;
    }
    
    
    
    
    
    
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section == 1) {
        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kImageH(30))];
        UILabel *discussLbl = [[UILabel alloc]initWithFrame:CGRectMake(kImageW(10), kImageH(5), kImageW(200), kImageH(30))];
        view.backgroundColor = [UIColor colorWithWhite:0.700 alpha:1.000];
        discussLbl.font = [UIFont systemFontOfSize:20];
        discussLbl.text = @"精选影评";
        [view addSubview:discussLbl];
        return view;
    }
    else
    {
        return nil;
    }
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 1) {
        return 30;
        
    }
    else
    {
        return 0;
    }
}
- (void)setDisValueWithCell:(FPDiscussTableViewCell *)cell indexpath:(NSIndexPath *)indexpath
{
    FPDisscussModel * model = _discussArr[indexpath.row];
    NSLog(@"%@",_discussArr);
    cell.titlelbl.text = model.title;
    NSLog(@"+++++++%@",model.content);
    cell.contentlbl.text = model.content;
    if ([_viewModel.movieID isEqualToString:@"203642"]) {
        
        NSString *str = [model.content substringFromIndex:12 ];
        cell.contentlbl.text = str;
    }
    NSURL *url = [NSURL URLWithString:model.headurl];
    [cell.headUrl sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"45545363.jpg"]];
    cell.nicknamelbl.text = model.nickname;
   cell.modifyTimelbl.text = [self setThatTimeWithDateString:model.modifyTime];
    NSLog(@"%@",model.rating);
    NSString *str = [NSString stringWithFormat:@"%@",model.rating];
    if (str.length > 5) {
        
        str = [str substringToIndex:3];
    }
    cell.ratinglbl.text = str;
    
    
    
    
}
- (NSString *)setThatTimeWithDateString:(NSString *)str
{
    NSInteger thatTime = [str integerValue];
    NSDate *thatDate = [NSDate dateWithTimeIntervalSince1970:thatTime];
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc]init];
    [dateFormat setDateFormat:@"YYYY-MM-dd HH:mm"];
    
    NSString *strReally = [dateFormat stringFromDate:thatDate];
    
    return strReally;
}
- (NSString *)setReallyString:(NSString *)str
{    int count = (int)[str length];
    NSLog(@"%d",count);
    for (int i = 0; i < count; i ++) {
        char c = [str characterAtIndex:i];
        NSLog(@"======%c===",c);
    }
    
    return str;
}
- (void)setCellValueWithCell:(FPdirectorTableViewCell *)cell
{
    NSURL *dirurl = [NSURL URLWithString:_direModel.directorImage];
    NSURL *act1url = [NSURL URLWithString:_direModel.actor1Image];
    NSURL *act2url = [NSURL URLWithString:_direModel.actor2Image];
    NSLog(@"%@",_direModel.actor1Roleimage);
    NSLog(@"%@", _direModel.actor2Roleimage);
    NSURL *act1roUrl = [NSURL URLWithString:_direModel.actor1Roleimage];
    NSURL *act2roUrl = [NSURL URLWithString:_direModel.actor2Roleimage];
    NSLog(@"%@",act1roUrl);
    NSLog(@"%@",act2roUrl);
    [cell.directorImage sd_setImageWithURL:dirurl placeholderImage:[UIImage imageNamed:@"noImage"]];
    [cell.actor1image sd_setImageWithURL:act1url placeholderImage:[UIImage imageNamed:@"noImage"]];
    [cell.actor2image sd_setImageWithURL:act2url placeholderImage:[UIImage imageNamed:@"noImage"]];
    [cell.actor1RoleImage sd_setImageWithURL:act1roUrl placeholderImage:[UIImage imageNamed:@"noImage"]];
    [cell.actor2RoleImage sd_setImageWithURL:act2roUrl placeholderImage:[UIImage imageNamed:@"noImage"]];
    
    
    cell.directorNamelbl.text = _direModel.directorName;
    cell.directorENlbl.text = _direModel.directorEn;
    if ([_direModel.produced1Name isEqualToString:@""]) {
        
        cell.productor1Namelbl.text = _direModel.producedNameEN;
    }
    else
    {
        cell.productor1Namelbl.text = _direModel.produced1Name;
    }
    cell.writer1Namelbl.text = _direModel.writer1Name;
    cell.actor1Namelbl.text = _direModel.actor1Name;
    cell.actor2Namelbl.text = _direModel.actor2Name;
    cell.personate1Name.text = [NSString stringWithFormat:@"饰:%@",_direModel.personate1Name];
    cell.personate2Name.text = [NSString stringWithFormat:@"饰:%@",_direModel.personate2Name];
    
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    
    if (scrollView.contentOffset.y < kImageH(50)) {
        self.detailV.tableView.bounces = NO;
        
    }
    else
    {
        self.detailV.tableView.bounces = YES;
        
    }
    // NSLog(@"%f",scrollView.contentOffset.y);
    UIColor *color = [UIColor colorWithRed:0.180 green:0.596 blue:1.000 alpha:1.000];
    CGFloat offsetY = scrollView.contentOffset.y;
    self.statusBarView.frame =CGRectMake(0, 0, kScreenWidth  , 20);
    if (offsetY > 0) {
        CGFloat alpha = 1 - (44 - offsetY) / 44;
        self.navigationController.navigationBar.backgroundColor = [color colorWithAlphaComponent:alpha];
        // [[UINavigationBar appearance]setBarTintColor:[color colorWithAlphaComponent:alpha]];
        
        _statusBarView.backgroundColor = [color colorWithAlphaComponent:alpha];
        
        [self setNeedsStatusBarAppearanceUpdate];
        [self.view addSubview:_statusBarView];
        
        
    }
    else {
        self.navigationController.navigationBar.backgroundColor = [color colorWithAlphaComponent:0];
        
        _statusBarView.backgroundColor = [color colorWithAlphaComponent:0];
        [self setNeedsStatusBarAppearanceUpdate];
        
        [self.view addSubview:_statusBarView];
    }
    
}

- (void)setview
{
    
    NSURL *url = [NSURL URLWithString:_viewModel.imgstr];
    [self.detailV.visual sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"296b467c57933f291a29e2c2255af022.jpg"]];
    [self.detailV.movieImage sd_setImageWithURL:url placeholderImage:[UIImage imageNamed:@"296b467c57933f291a29e2c2255af022.jpg"]];
    self.detailV.titleCn.text = _viewModel.titleCn;
    self.detailV.titleEn.text = _viewModel.titleEn;
    NSString *str = [_viewModel.ratingFinal substringToIndex:3];
    self.detailV.ratingFinal.text = [NSString stringWithFormat:@"网上评分: %@",str];
    self.detailV.actor.text = [NSString stringWithFormat:@"%@,%@",_viewModel.actor1,_viewModel.actor2];
    self.detailV.directorname.text = [NSString stringWithFormat:@"导演: %@",_viewModel.directorName];
    if (_viewModel.isPass == NO)
    {
         self.detailV.timeShow.text = [NSString stringWithFormat:@"%@年%@月%@日中国上映",_viewModel.rYear,_viewModel.rMonth,_viewModel.rDay];
    }
    else
    {
        self.detailV.timeShow.text = [NSString stringWithFormat:@"上映时间:%@",_viewModel.time];
    }
   
    self.detailV.actorWord.text = _viewModel.actorWord;
    self.detailV.tableView.delegate = self;
    self.detailV.tableView.dataSource = self;
   
    [self.detailV.tableView registerClass:[FPdirectorTableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.detailV.tableView registerClass:[FPDiscussTableViewCell class] forCellReuseIdentifier:@"cell1"];
}
- (void)addTarget
{
    [self.detailV.heartBtn addTarget:self action:@selector(heartBtn) forControlEvents:UIControlEventTouchUpInside];
    [self.detailV.shareBtn addTarget:self action:@selector(shareBtn) forControlEvents:UIControlEventTouchUpInside];
}
- (void)heartBtn
{
    
}
- (void)shareBtn
{
    [UMSocialSnsService presentSnsIconSheetView:self
                                         appKey:@"55acadd6e0f55a7e410046b0"
                                      shareText:@"友盟社会化分享让您快速实现分享等社会化功能，http://umeng.com/social"
                                     shareImage:[UIImage imageNamed:@"icon"]
                                shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,UMShareToWechatTimeline,UMShareToSms,UMShareToFacebook,UMShareToEmail,UMShareToTencent,UMShareToRenren,nil]
                                       delegate:self];
}
- (FPDetailMovie *)detailV
{
    if (!_detailV) {
        _detailV = [[FPDetailMovie alloc]init];
        self.view = _detailV;
    }
    return _detailV;
}
- (UIView *)statusBarView
{
    if (!_statusBarView) {
        _statusBarView = [[UIView alloc]init];
        [self.view addSubview:_statusBarView];
    }
    return _statusBarView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
